home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / ge_cool.lha / GE_COOL2.1 / src / Complex / tests / test_Complex.C < prev   
C/C++ Source or Header  |  1992-07-14  |  8KB  |  241 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12.  
  13. #include <cool/Complex.h>
  14. #include <test.h>
  15.  
  16. void test_complex() {
  17.   CoolComplex c1;
  18.   TEST ("Complex c1", c1.status(), N_OK);
  19.   TEST ("c1.modulus", c1.modulus()==0, TRUE);
  20.   CoolComplex c2(3.0,5.0);
  21.   TEST ("Complex c2(3.0,5.0)", c2.status(), N_OK);
  22.   TEST ("c2.real()", c2.real(), 3.0);
  23.   TEST ("c2.imaginary()", c2.imaginary(), 5.0);
  24.   TEST ("c1 != c2", c1 != c2, TRUE);
  25.   TEST ("c2.modulus", c2.modulus()==sqrt(34.0), TRUE);
  26.   TEST ("c2.argument", c2.argument()==atan2(5.0,3.0), TRUE);
  27.   CoolComplex c3(9.0);
  28.   TEST ("Complex c3(9.0)", c3.status(), N_OK);
  29.   TEST ("c3.real()", c3.real(), 9.0);
  30.   TEST ("c3.imaginary()", c3.imaginary(), 0.0);
  31.   TEST ("c3.modulus", c3.modulus()==9.0, TRUE);
  32.   TEST ("c3.argument", c3.argument()==0.0, TRUE);
  33.   CoolComplex c4(6,10);
  34.   TEST ("Complex c4(6,10)", c4.status(), N_OK);
  35.   TEST ("c4.real()", c4.real(), 6.0);
  36.   TEST ("c4.imaginary()", c4.imaginary(), 10.0);
  37.   TEST ("c2 == c4", c2 == c4, FALSE);
  38.   TEST ("c1 = c2", c1.operator=(c2), c2);
  39.   TEST ("c1.status()", c1.status(), N_OK);
  40.   TEST ("c1.real()", c1.real(), 3.0);
  41.   TEST ("c1.imaginary()", c1.imaginary(), 5.0);
  42.   TEST ("c1 == c4", c1 == c4, FALSE);
  43.   TEST ("-c3",(c1 = -c3,(c1.real() == -9.0 && c1.imaginary() == 0.0)),TRUE);
  44.   TEST ("c1 = -c3", (c1.real() == -9.0 && c1.imaginary() == 0.0), TRUE);
  45.   CoolComplex c5(c4);
  46.   TEST ("Complex c5(c4)", c4 == c5, TRUE);
  47.   TEST ("!c5", !c5, FALSE);
  48.   TEST ("c5 = c2", (c5 = c2, c5 == c2), TRUE);
  49.   TEST ("c5--", (c5--, c5.real() == 2.0 && c5.imaginary() == 5.0), TRUE);
  50.   TEST ("c5++", (c5++, c5.real() == 3.0 && c5.imaginary() == 5.0), TRUE);
  51.   c5 = c5.invert ();
  52.   TEST ("c5 = c5.invert ()", (c5.real()<=0.089 && c5.real() >= 0.088 &&
  53.        c5.imaginary()<=-0.14 && c5.imaginary()>=-0.15), TRUE);
  54.   TEST ("c1=c2+c4",(c1=c2+c4,c1.real()==9.0 && c1.imaginary()==15.0),TRUE);
  55.   TEST ("c1=c2-c4",(c1=c2-c4,c1.real()==-3.0 && c1.imaginary()==-5.0),TRUE);
  56.   TEST ("c1=c2*c4",(c1=c2*c4,c1.real()==-32.0 && c1.imaginary()==60.0),TRUE);
  57.   TEST ("c1=c2/c4",(c1=c2/c4,c1.real()==0.5 && c1.imaginary()==0.0),TRUE);
  58.   TEST ("c2+=c4",(c2+=c4,c2.real()==9.0 && c2.imaginary()==15.0),TRUE);
  59.   TEST ("c2-=c4",(c2-=c4,c2.real()==3.0 && c2.imaginary()==5.0),TRUE);
  60.   TEST ("c2*=c4",(c2*=c4,c2.real()==-32.0 && c2.imaginary()==-290.0),TRUE);
  61.   TEST ("c2/=c4",(c2/=c4, (c2.real()<=-22.73 && c2.real() >= -22.74 &&
  62.            c2.imaginary()<=-10.44 && c2.imaginary()>=-10.45)),TRUE);
  63. }
  64.  
  65.  
  66. void test_linear () {
  67.   CoolComplex r1, c1;
  68.   TEST ("0*x+0", 
  69.     (c1=0, CoolComplex::roots_of_linear(0, 0, r1)==1 && 
  70.      r1==c1), TRUE);
  71.   TEST ("0*x+1", 
  72.     (CoolComplex::roots_of_linear(0, 1, r1)==0), TRUE);
  73.   TEST ("3*x+0", 
  74.     (c1=0, CoolComplex::roots_of_linear(3, 0, r1)==1 && 
  75.      r1==c1), TRUE);
  76.   TEST ("3*x+2", 
  77.     (c1=(-2.0/3.0), CoolComplex::roots_of_linear(3, 2, r1)==1 && 
  78.      r1==c1), TRUE);
  79. }
  80.  
  81. void test_quadratic () {
  82.   CoolComplex r1, r2, c1, c2;
  83.   TEST ("0*x^2+0*x+0", 
  84.     (c1=0, CoolComplex::roots_of_quadratic(0, 0, 0, r1, r2)==1 && 
  85.      r1==c1), TRUE);
  86.   TEST ("0*x^2+0*x+1", 
  87.     (CoolComplex::roots_of_quadratic(0, 0, 1, r1, r2)==0), TRUE);
  88.   TEST ("3*x^2+2*x+0", 
  89.     (c1=(-2.0/3.0), c2=0,
  90.      CoolComplex::roots_of_quadratic(3, 2, 0, r1, r2)==2 &&
  91.      r1==c1 && 
  92.      r2==c2), TRUE);
  93.   TEST ("x^2+2*x+1", 
  94.     (c1=-1, c2=-1, CoolComplex::roots_of_quadratic(1, 2, 1, r1, r2)==2 &&
  95.      r1==c1 && 
  96.      r2==c2), TRUE);
  97.   TEST ("x^2+3*x+2", 
  98.     (c1=-2, c2=-1, CoolComplex::roots_of_quadratic(1, 3, 2, r1, r2)==2 &&
  99.      r1==c1 && 
  100.      r2==c2), TRUE);
  101.   TEST ("x^2-2*x+2", 
  102.     (c1=CoolComplex(1,1), c2=CoolComplex(1,-1),
  103.      CoolComplex::roots_of_quadratic(1, -2, 2, r1, r2)==2 &&
  104.      r1==c1 && 
  105.      r2==c2), TRUE);
  106.   TEST ("x^2+1", 
  107.     (c1=CoolComplex(0,1), c2=CoolComplex(0,-1),
  108.      CoolComplex::roots_of_quadratic(1, 0, 1, r1, r2)==2 &&
  109.      r1==c1 && 
  110.      r2==c2), TRUE);
  111. }
  112.  
  113. void test_cubic () {
  114.   CoolComplex r1, r2, r3, c1, c2, c3;
  115.   TEST ("0*x^3+0*x^2+0*x+0", 
  116.     (c1=0, CoolComplex::roots_of_cubic(0, 0, 0, 0, r1, r2, r3)==1 && 
  117.      r1==c1), TRUE);
  118.   TEST ("0*x^3+0*x^2+0*x+1", 
  119.     (CoolComplex::roots_of_cubic(0, 0, 0, 1, r1, r2, r3)==0), TRUE);
  120.   TEST ("x^3+3*x^2+2*x", 
  121.     (c1=-2, c2=-1, c3=0,
  122.      CoolComplex::roots_of_cubic(1, 3, 2, 0, r1, r2, r3)==3 &&
  123.      r1==c1 && 
  124.      r2==c2 &&
  125.      r3==c3), TRUE);
  126.   TEST ("x^3+x", 
  127.     (c1=CoolComplex(0,1), c2=CoolComplex(0,-1), c3=0,
  128.      CoolComplex::roots_of_cubic(1, 0, 1, 0, r1, r2, r3)==3 &&
  129.      r1==c1 && 
  130.      r2==c2 &&
  131.      r3==c3), TRUE);
  132.   TEST ("x^3-3*x^2+3*x-1", 
  133.     (c1=1, c2=1, c3=1,
  134.      CoolComplex::roots_of_cubic(1, -3, 3, -1, r1, r2, r3)==3 &&
  135.      r1==c1 && 
  136.      r2==c2 &&
  137.      r3==c3), TRUE);
  138.   TEST ("x^3-4*x^2+5*x-2", 
  139.     (c1=2, c2=1, c3=1,
  140.      CoolComplex::roots_of_cubic(1, -4, 5, -2, r1, r2, r3)==3 &&
  141.      r1==c1 && 
  142.      r2==c2 && 
  143.      r3==c3), TRUE);
  144.   TEST ("x^3-2*x^2-x+2", 
  145.     (c1=2, c2=1, c3=-1,
  146.      CoolComplex::roots_of_cubic(1, -2, -1, 2, r1, r2, r3)==3 &&
  147.      r1==c1 && 
  148.      r2==c2 && 
  149.      r3==c3), TRUE);
  150.   TEST ("x^3-x^2+x-1", 
  151.     (c1=1, c2=CoolComplex(0,1), c3=CoolComplex(0,-1),
  152.      CoolComplex::roots_of_cubic(1, -1, 1, -1, r1, r2, r3)==3 &&
  153.      fabs(r1.real()-1)<1.0e-12 && fabs(r1.imaginary()-0)<1.0e-12 &&
  154.      fabs(r2.real()-0)<1.0e-12 && fabs(r2.imaginary()-1)<1.0e-12 &&
  155.      fabs(r3.real()-0)<1.0e-12 && fabs(r3.imaginary()+1)<1.0e-12
  156.      ), TRUE);
  157.   TEST ("x^3-x^2+2", 
  158.     (c1=-1, c2=CoolComplex(1,1), c3=CoolComplex(1,-1),
  159.      CoolComplex::roots_of_cubic(1, -1, 0, 2, r1, r2, r3)==3 &&
  160.      fabs(r1.real()+1)<1.0e-12 && fabs(r1.imaginary()-0)<1.0e-12 &&
  161.      fabs(r2.real()-1)<1.0e-12 && fabs(r2.imaginary()-1)<1.0e-12 &&
  162.      fabs(r3.real()-1)<1.0e-12 && fabs(r3.imaginary()+1)<1.0e-12
  163.      ), TRUE);
  164. }
  165.  
  166. void test_quartic () {
  167.   CoolComplex r1, r2, r3, r4, c1, c2, c3, c4;
  168.   TEST ("0*x^4+0*x^3+0*x^2+0*x+0", 
  169.     (c1=0, CoolComplex::roots_of_quartic(0, 0, 0, 0, 0, r1, r2, r3, r4)==1 && 
  170.      r1==c1), TRUE);
  171.   TEST ("0*x^4+0*x^3+0*x^2+0*x+1", 
  172.     (CoolComplex::roots_of_quartic(0, 0, 0, 0, 1, r1, r2, r3, r4)==0), TRUE);
  173.   TEST ("x^4-2*x^3-x^2+2*x", 
  174.     (c1=2, c2=1, c3=-1, c4=0,
  175.      CoolComplex::roots_of_quartic(1, -2, -1, 2, 0, r1, r2, r3, r4)==4 &&
  176.      r1==c1 && 
  177.      r2==c2 && 
  178.      r3==c3 &&
  179.      r4==c4), TRUE);
  180.   TEST ("x^4+x^2", 
  181.     (c1=CoolComplex(0,1), c2=CoolComplex(0,-1), c3=0, c4=0,
  182.      CoolComplex::roots_of_quartic(1, 0, 1, 0, 0, r1, r2, r3, r4)==4 &&
  183.      r1==c1 && 
  184.      r2==c2 &&
  185.      r3==c3 &&
  186.      r4==c4), TRUE);
  187.   TEST ("x^4+4*x^3+6*x^2+4*x+1",
  188.     (c1=-1, c2=-1, c3=-1, c4=-1,
  189.      CoolComplex::roots_of_quartic(1, 4, 6, 4, 1, r1, r2, r3, r4)==4 &&
  190.      r1==c1 && 
  191.      r2==c2 &&
  192.      r3==c3 &&
  193.      r4==c4), TRUE);
  194.   TEST ("x^4-4*x^3+6*x^2-4*x+1",
  195.     (c1=+1, c2=+1, c3=+1, c4=+1,
  196.      CoolComplex::roots_of_quartic(1, -4, 6, -4, 1, r1, r2, r3, r4)==4 &&
  197.      r1==c1 && 
  198.      r2==c2 &&
  199.      r3==c3 &&
  200.      r4==c4), TRUE);
  201.   TEST ("x^4-2*x^3+2*x^2-2*x+1",
  202.     (c1=+1, c2=+1, c3=CoolComplex(0,1), c4=CoolComplex(0,-1),
  203.      CoolComplex::roots_of_quartic(1, -2, 2, -2, 1, r1, r2, r3, r4)==4 &&
  204.      r1==c1 && 
  205.      r2==c2 &&
  206.      r3==c3 &&
  207.      r4==c4), TRUE);
  208.   TEST ("x^4+x^3-2*x^2+2*x+4", 
  209.     (c1=CoolComplex(1,1), c2=CoolComplex(1,-1), c3=-2, c2=-1, 
  210.      CoolComplex::roots_of_quartic(1, 1, -2, 2, 4, r1, r2, r3, r4)==4 &&
  211.      fabs(r1.real()-1)<1.0e-12 && fabs(r1.imaginary()-1)<1.0e-12 &&
  212.      fabs(r2.real()-1)<1.0e-12 && fabs(r2.imaginary()+1)<1.0e-12 &&
  213.      fabs(r3.real()+2)<1.0e-12 && fabs(r3.imaginary()-0)<1.0e-12 &&
  214.      fabs(r4.real()+1)<1.0e-12 && fabs(r4.imaginary()-0)<1.0e-12
  215.      ), TRUE);
  216. }
  217.  
  218. void test_leak () {
  219.   for (;;) {
  220.     test_complex();
  221.     test_linear();
  222.     test_quadratic();
  223.     test_cubic();
  224.     test_quartic();
  225.   }
  226. }
  227.  
  228. int main (void) {
  229.   START("CoolComplex");
  230.   test_complex();
  231.   test_linear();
  232.   test_quadratic();
  233.   test_cubic();
  234.   test_quartic();
  235. #if LEAK
  236.   test_leak();
  237. #endif
  238.   SUMMARY();
  239.   return 0;
  240. }
  241.